const router = require('koa-router')();
const DB = require('../../module/db.js');
const tools = require('../../module/tools.js');

router.get('/', async (ctx) => {

	let result = await DB.findOne('admin', {});
    await ctx.render('admin/manage/list', {
    	list: result
    });
});



router.get('/add', async (ctx) => {

    await ctx.render('admin/manage/add');
});

router.post('/add', async (ctx) => {

    // ctx.request.body
    let username = ctx.request.body.username;
    let password = ctx.request.body.password;
    let rPassword = ctx.request.body.rpassword;
    let status = parseInt(ctx.request.body.status, 0);

    if(/^\w{4, 20}/.test(username)){
        await ctx.render('admin/error', {
            message: '用户名不合法.',
            redirectUrl: ctx.state.__HOST__ + '/admin/manage/add'
        });
        return;
    }
    if(password.length < 6){
        await ctx.render('admin/error', {
            message: '密码长度小于6位.',
            redirectUrl: ctx.state.__HOST__ + '/admin/manage/add'
        });
        return;
    }
    if(password !== rPassword) {
        await ctx.render('admin/error', {
            message: '密码和确认密码不一致.',
            redirectUrl: ctx.state.__HOST__ + '/admin/manage/add'
        });
        return;
    }

    // 查询当前数据是否存在
    var findResult = await DB.findOne('admin', {
        username: username
    });

    if(findResult.length > 0){
        await ctx.render('admin/error', {
            message: '增加失败，管理员已存在.',
            redirectUrl: ctx.state.__HOST__ + '/admin/manage/add'
        });
        return;
    }

    var addResult = await DB.insertOne('admin', {
        username: username,
        password: tools.md5(password),
        pic: '',
        create_time: new Date(),
        last_time: '',
        status: status,
    });

    ctx.redirect(ctx.state.__HOST__ + '/admin/manage');
});



router.get('/edit', async (ctx) => {

    let id = ctx.query.id;
    let result = await DB.findOne('admin', {
        _id: DB.getObjectID(id)
    });

    await ctx.render('admin/manage/edit', {
        list: result[0]
    });
});

router.post('/edit', async (ctx) => {

    // ctx.request.body
    let id = ctx.request.id;
    let username = ctx.request.body.username;
    let password = ctx.request.body.password;
    let rPassword = ctx.request.body.rpassword;
    let status = parseInt(ctx.request.body.status, 0);

    if(password === ''){
        ctx.redirect(ctx.state.__HOST__ + '/admin/manage');
        return;
    }
    if(password.length < 6){
        await ctx.render('admin/error', {
            message: '密码长度小于6位.',
            redirectUrl: ctx.state.__HOST__ + '/admin/manage/edit?id=' + id
        });
        return;
    }
    if(password !== rPassword) {
        await ctx.render('admin/error', {
            message: '密码和确认密码不一致.',
            redirectUrl: ctx.state.__HOST__ + '/admin/manage/edit?id=' + id
        });
        return;
    }

    let updateResult = await DB.updateOne('admin', {
        _id: DB.getObjectID(id)
    }, {
        password: tools.md5(password),
        status: status,
    });

    ctx.redirect(ctx.state.__HOST__ + '/admin/manage');
});


router.get('/delete', async (ctx) => {

    ctx.body = '删除用户';
});

module.exports = router.routes();